﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace GJXQGame
{
    public class ChessControl
    {


        private GameObject _Pointer;

        public GameObject Pointer
        {
            set
            {
                _Pointer = value;
                PlayInfoSelectTag(value);
            }
            get
            {
                return _Pointer;
            }
        }
        
        public GameObject Selected;

        bool IsChange = false;

        public float MoveChessTime=0.2f;
        public void MoveChess()
        {
            ChessPieseSlot slot = Pointer.GetComponent<ChessPieseSlot>();
            ChessPieseSlot slotTag = Selected.GetComponent<ChessPieseSlot>();
            if (slot.Type==slotTag.Type)
            {
                ShowTitle("不能这样移动");
                ClearnObjTag(false);
                return;
            }
            if (slot.JewelPower==PowerType.KING)
            {
                if ((slotTag.JewelPosition.Point_x == slot.JewelPosition.Point_x && Math.Abs(slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y) == 1) || (slotTag.JewelPosition.Point_y == slot.JewelPosition.Point_y && Math.Abs(slotTag.JewelPosition.Point_x - slot.JewelPosition.Point_x) == 1)|| (Math.Abs(slotTag.JewelPosition.Point_x - slot.JewelPosition.Point_x) == 1 && Math.Abs(slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y) == 1))
                {
                    ChangeChess();
                }
            }
            else if (slot.JewelPower == PowerType.QING)
            {
                if ((slotTag.JewelPosition.Point_x == slot.JewelPosition.Point_x && Math.Abs(slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y) >= 1) && HaveOtherChess(slot, slotTag, 2) || (slotTag.JewelPosition.Point_y == slot.JewelPosition.Point_y && Math.Abs(slotTag.JewelPosition.Point_x - slot.JewelPosition.Point_x) >= 1) && HaveOtherChess(slot, slotTag, 1) || (Math.Abs(slotTag.JewelPosition.Point_x - slot.JewelPosition.Point_x) == Math.Abs(slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y)) && HaveOtherChess(slot, slotTag, 3))
                {
                    ChangeChess();
                }
            }
            else if (slot.JewelPower == PowerType.ROOK)
            {
                if ((slotTag.JewelPosition.Point_x == slot.JewelPosition.Point_x && Math.Abs(slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y) >= 1) && HaveOtherChess(slot, slotTag, 2) || (slotTag.JewelPosition.Point_y == slot.JewelPosition.Point_y && Math.Abs(slotTag.JewelPosition.Point_x - slot.JewelPosition.Point_x) >= 1) && HaveOtherChess(slot, slotTag, 1))
                {
                    ChangeChess();
                }
            }
            else if (slot.JewelPower == PowerType.BISHOP)
            {
                if ((Math.Abs(slotTag.JewelPosition.Point_x - slot.JewelPosition.Point_x) == Math.Abs(slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y)) && HaveOtherChess(slot, slotTag, 3))
                {
                    ChangeChess();
                }
            }
            else if (slot.JewelPower == PowerType.KNIGHT)
            {
                if ((Math.Abs(slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y) == 1 && Math.Abs(slotTag.JewelPosition.Point_x - slot.JewelPosition.Point_x) == 2) || (Math.Abs(slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y) == 2 && Math.Abs(slotTag.JewelPosition.Point_x - slot.JewelPosition.Point_x) == 1))
                {
                    ChangeChess();
                }
            }
            else if (slot.JewelPower == PowerType.PAWN)
            {
                if (slotTag.JewelPower!=PowerType.NONE)
                {
                    if ((Math.Abs(slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y) == 1 && Math.Abs(slotTag.JewelPosition.Point_x - slot.JewelPosition.Point_x) == 1))
                    {
                        ChangeChess();
                    }
                }
                else
                {
                    if (slotTag.JewelPosition.Point_x == slot.JewelPosition.Point_x && (slotTag.JewelPosition.Point_y - slot.JewelPosition.Point_y)==1)
                    {
                        ChangeChess();
                    }
                }
            }
            if (!IsChange)
            {
                ShowTitle("不能这样移动");
                ClearnObjTag(false);
            }
            IsChange = false;
        }
        /// <summary>
        /// 中间隔短棋子 1  直线 x  2 直线 y  3 斜线  4 all
        /// </summary>
        public bool HaveOtherChess(ChessPieseSlot tag1 ,ChessPieseSlot tag2 ,int Type)
        {
            DateModelControl.Point t1 = tag1.JewelPosition;
            DateModelControl.Point t2 = tag2.JewelPosition;
            bool tag = true;
            switch (Type)
            {
                case 1:
                    {
                        if (t1.Point_x<t2.Point_x)
                        {
                            tag = TagFor(t1, t2, Type);
                        }
                        else
                        {
                            tag = TagFor(t2, t1, Type);
                        }
                    }break;
                case 2:
                    {
                        if (t1.Point_y < t2.Point_y)
                        {
                            tag = TagFor(t1, t2, Type);
                        }
                        else
                        {
                            tag = TagFor(t2, t1, Type);
                        }
                    } break;
                case 3:
                    {
                        tag = TagFor(t1, t2, Type);
                    } break;
            }
            Debug.LogError(tag);
            return tag;
        }
        /// <summary>
        /// 是否隔断
        /// </summary>
        /// <param name="t1"></param>  小   
        /// <param name="t2"></param>  大
        /// <returns></returns>
        public bool TagFor(DateModelControl.Point t1 ,DateModelControl.Point t2,int Type)
        {
            if (Type==1)
            {
                for (int i = t1.Point_x + 1; i < t2.Point_x; i++)
                {
                    ChessPieseSlot slot = PlayInfoData.GetInstance().mLastMapThird[i, t1.Point_y].GetComponent<ChessPieseSlot>();
                    if (slot.Type!=TypePlay.None)
                    {
                        return false;
                    }
                }
            }
            else if (Type == 2)
            {
                for (int i = t1.Point_y + 1; i < t2.Point_y; i++)
                {
                    ChessPieseSlot slot = PlayInfoData.GetInstance().mLastMapThird[t1.Point_x, i].GetComponent<ChessPieseSlot>();
                    if (slot.Type != TypePlay.None)
                    {
                        return false;
                    }
                }
            }
            else if (Type == 3)
            {
                int r = 1;
                int c = 1;
                if (t1.Point_x>t2.Point_x)
                {
                    r = -1;
                }
                if (t1.Point_y>t2.Point_y)
                {
                    c = -1;
                }
                DateModelControl.Point startpoint = t1;
                for (int i = t1.Point_y + 1; i < Math.Abs(t1.Point_y-t2.Point_y); i++)
                {
                    ChessPieseSlot slot = PlayInfoData.GetInstance().mLastMapThird[startpoint.Point_x += r, startpoint.Point_y+=c].GetComponent<ChessPieseSlot>();
                    if (slot.Type != TypePlay.None)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public void ChangeChess()
        {
            IsChange = true;
            Selected.GetComponent<ChessPieseSlot>().JewelPower = Pointer.GetComponent<ChessPieseSlot>().JewelPower;
            Selected.GetComponent<ChessPieseSlot>().Type = Pointer.GetComponent<ChessPieseSlot>().Type;
            //Selected.GetComponent<ChessPieseSlot>().RefShow();
            Pointer.GetComponent<ChessPieseSlot>().JewelPower = 0;
            Pointer.GetComponent<ChessPieseSlot>().Type = TypePlay.None;
            GameObject NewflyObj = GameObject.Instantiate(Pointer);
            Pointer.GetComponent<ChessPieseSlot>().RefShow();
            //NewflyObj.transform.SetParent(Pointer.transform.parent);
            NewflyObj.transform.parent = Pointer.transform.parent;
            NewflyObj.transform.localPosition = Pointer.transform.localPosition;
            NewflyObj.transform.localScale = Vector3.one;
            iTween.MoveTo(NewflyObj, iTween.Hash("position", Selected.transform.position, "time", MoveChessTime-0.05f, "easetype", iTween.EaseType.linear));
            GameRoot.Destroy(NewflyObj, MoveChessTime+0.05f);
            GameRoot.StartCoroutine(WaitTime(Pointer.GetComponent<ChessPieseSlot>(), Selected.GetComponent<ChessPieseSlot>(), MoveChessTime));
            GameManager.ChessControl.ShowCover();
        }


        IEnumerator WaitTime(ChessPieseSlot tag,ChessPieseSlot tag1, float time)
        {
            yield return new WaitForSeconds(time);
            //tag.RefShow();
            tag1.RefShow();
            ClearnObjTag();
        }

        public void ClearnObjTag(bool IsChange=true)
        {
            Pointer = null;
            Selected = null;
            if (IsChange)
            {
                GameManager.ChessDataControl.GetMyChess();
                GameManager.ChessDataControl.GetOtherChess();
            }
        }

        public void ShowTitle(string Str)
        {
            GameObject slotItemClone = GameObject.Instantiate(Resources.Load<GameObject>("Windows/Prefab/ShowTitlePanel"));
            slotItemClone.transform.SetParent(GUIManager.GetInstance().GetRoot().transform);
            slotItemClone.transform.localScale = Vector3.one;
            slotItemClone.transform.FindChild("ShowTitle").GetComponent<UILabel>().text = Str;
            GameRoot.Destroy(slotItemClone,2);

        }

        public void ShowCover()
        {
            GameObject slotItemClone = GameObject.Instantiate(Resources.Load<GameObject>("Windows/Prefab/ConverPanel"));
            slotItemClone.transform.SetParent(GameManager.GUIManager.GetGUI(GUI_ID.GUI_ID_PLAYINFOWIN).GameObject.transform);
            slotItemClone.transform.localScale = Vector3.one;
            GameRoot.Destroy(slotItemClone,MoveChessTime);
        }

        public void PlayInfoSelectTag(GameObject Obj)
        {
            if (Obj)
            {
                DateModelControl.Point v1 = Obj.GetComponent<ChessPieseSlot>().JewelPosition;

                GameManager.GUIManager.GetGUI(GUI_ID.GUI_ID_PLAYINFOWIN).GameObject.transform.FindChild("PlayInfoWin/SelectSpr").gameObject.transform.localPosition = new Vector3((v1.Point_x - 4) * 80 + 40, (v1.Point_y - 4) * 80 + 40 + 3);
                GameManager.GUIManager.GetGUI(GUI_ID.GUI_ID_PLAYINFOWIN).GameObject.transform.FindChild("PlayInfoWin/SelectSpr").gameObject.SetActive(true);
            }
            else
            {
                GameManager.GUIManager.GetGUI(GUI_ID.GUI_ID_PLAYINFOWIN).GameObject.transform.FindChild("PlayInfoWin/SelectSpr").gameObject.SetActive(false);
            }
        }
    }
}
